﻿<style>
    .table2 .layui-table-cell {
        height: 26px;
        font-size: 0.9em;
    }
    .bksp{
        color:white;
        background:#FF5722;
        border:2px solid #FF5722;
        border-radius:3px;
        cursor:pointer;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list2">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">关键字</label>
                    <div class="layui-input-inline">
                        <input type="text" name="key" id="key" placeholder="店铺名称/商品编码" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">店铺县区</label>
                    <div class="layui-input-inline">
                        <select name='storeArea' id='storeArea' lay-verify='' lay-search>
                            <option value=""></option>
                        </select>
                    </div>
                    <label class="layui-form-label">商品状态</label>
                    <div class="layui-input-inline">
                        <select name='show' id='show' lay-search>
                            <option value=""></option>
                            <option value="0">上架</option>
                            <option value="1">下架</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <input type="checkbox" name="groupbuy" lay-skin="switch" lay-filter="switchTest3" lay-text="团购|团购">
                    <button class="layui-btn layuiadmin-btn-list2" lay-submit lay-filter="LAY-app-contlist-search2">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body table2">
            <div style="padding-bottom: 10px;display:none" id="StoreGoodBtnDiv">
                <script type="text/html" template lay-done="layui.data.setActive2()">
                    <button class="layui-btn layuiadmin-btn-list2" data-type="del">删除</button>
                    <button class="layui-btn layuiadmin-btn-list2" data-type="setstock">设置库存</button>
                    <button class="layui-btn layuiadmin-btn-list2" data-type="showon">上架</button>
                    <button class="layui-btn layuiadmin-btn-list2" data-type="showup">下架</button>
                    <button class="layui-btn layuiadmin-btn-list2" data-type="groupbuy">加入团购</button>
                    <button class="layui-btn layuiadmin-btn-list2" data-type="cancelgroupbuy">取消团购</button>
                </script>
            </div>
            <table id="LAY-app-content-list2" lay-filter="LAY-app-content-list2"></table>
        </div>
    </div>
</div>
<script>
    var checkids = [];  //商品ID集合

    var form, admin, table, view, formSelects, $;
    layui.use(['admin', 'table', 'form', 'formSelects'], function () {

        $ = layui.$;
        admin = layui.admin;
        view = layui.view;
        table = layui.table;
        form = layui.form;
        formSelects = layui.formSelects;

        if (StoreGoodRight == "1") {
            $("#StoreGoodBtnDiv").show();
        } else {
            $("#StoreGoodBtnDiv").hide();
        }

        admin.req({
            url: layui.setter.APIUrl + '/shop/StoreareaMgr/customerQuery', //实际使用请改成服务端真实接口
            type: 'post',
            done: function (res) {
                layui.each(res.Data, function (index, eachdata) {
                    $("#storeArea").append("<option value='" + eachdata.Name + "'>" + eachdata.Name + "</option>");
                });
                form.render('select');
            }
        });


        form.render(null, 'app-content-list2');
        
        form.on('switch(switchTest3)', function (data) {
            var field = form.val("app-content-list2");
            field["groupbuy"] = data.elem.checked;
            //执行重载
            table.reload('LAY-app-content-list2', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });


        //文章管理
        table.render({
            elem: '#LAY-app-content-list2'
            , toolbar: false  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , defaultToolbar: ['filter']
            , method: "post"
            , url: layui.setter.APIUrl + '/shop/goodsMgr/listPain?baseGoodID=' + GoodID
            , headers: { //通过 request 头传递
                Authorization: "bearer " + layui.data('layuiAdmin').access_token
            }
            , parseData: function (res) { //res 即为原始返回的数据 
                return {
                    "ResultCode": res.ResultCode, //解析接口状态
                    "ResultInfo": res.ResultInfo, //解析提示文本
                    "count": res.Data.TotalRecordCount, //解析数据长度
                    "Data": res.Data.Items //解析数据列表 
                };
            }
        , cols: [[
            { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100      
            { field: 'StoreArea', title: '县区', minWidth: 120 },
            { field: 'StoreID_SelSourceName', title: '店铺', minWidth: 120 },
            { field: 'Code', title: '店铺商品编码', minWidth: 120 },
            {
                field: 'Stock', title: '店铺库存数量', minWidth: 70, templet: function (d) {
                    var html = "<span style='font-weight:bold;font-size:1.1em'>" + d.Stock + "</span>";
                    return html;
                }
            },
            {
                field: 'IsShow_Sel', title: '状态', minWidth: 70, templet: function (d) {
                    var Show = d.IsShow_Sel;
                    var str = "";
                    if (Show == 0) {
                        str = "<span style='color:#5FB878;font-weight:bold'>上架</span>";
                    } else {
                        str = "<span style='color:#c2c2c2;font-weight:bold'>下架</span>";
                    }
                    var html = "" + str;
                    return html;
                }
            },
            {
                field: 'Stock', title: '团购信息', minWidth: 70, templet: function (d) {
                    var html = "";
                    if (d.GroupBuyInfo != null) {
                        html = "<span class='bksp' id='tip" + d.Code + "' onclick=\"GroupBuyInfo('" + d.GroupBuyInfo.GroupPrice + "','" + d.GroupBuyInfo.StartTime + "','" + d.GroupBuyInfo.EndTime + "','" + d.GroupBuyInfo.IsAble_Sel + "', '" + d.Code + "')\">团购</span>";
                    }
                    return html;
                }
            },
        ]]
        , page: true
        , limit: 100
        , limits: [100]
        , text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
        }
        });


        //监听搜索
        form.on('submit(LAY-app-contlist-search2)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-app-content-list2', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        })

    });

    layui.data.setActive2 = function () {

        layui.use(['form', 'admin', 'table'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var view = layui.view;
            var table = layui.table;
            var active = {
                del: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list2')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    //return layer.msg('已删除');
                    layer.confirm('确定删除吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/BatchDelete' //实际使用请改成服务端真实接口
                          , data: { "ids": ids }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg(res.ResultInfo);
                              table.reload('LAY-app-content-list2');
                          }
                        });
                    });
                },
                //设置库存
                setstock: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list2')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);

                    //prompt层
                    layer.prompt({ title: '请输入库存数量', formType: 0 }, function (text, index) {
                        if (text == "" || text == null) {
                            layer.msg("请输入库存数量");
                            return false;
                        }
                        if (!(/(^[1-9]\d*$)/.test(text)) && text != 0) {
                            layer.msg("请输入正整数");
                            return
                        }
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/SetStock' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "qty": text }
                              , type: "post"
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("设置成功");
                              table.reload('LAY-app-content-list2');
                          }
                        });

                        layer.close(index);
                    });
                },
                //上架
                showon: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list2')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                        if (eobj.IsShow_Sel == 0) {
                            tip = "选择商品当前状态为上架，请勿勾选";
                        }
                    });
                    if (tip != "") {
                        return layer.msg(tip);
                    }
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定上架吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/GoodShow' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "show": 0 }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("上架成功");
                              table.reload('LAY-app-content-list2');
                          }
                        });
                    });
                },
                //下架
                showup: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list2')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                        if (eobj.IsShow_Sel == 1) {
                            tip = "选择商品当前状态为下架，请勿勾选";
                        }
                    });
                    if (tip != "") {
                        return layer.msg(tip);
                    }
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定下架吗？下架后商城中将不可见。', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/goodsMgr/GoodShow' //实际使用请改成服务端真实接口
                          , data: { "ids": ids, "show": 1 }
                              , type: "post"
                            //, success: function (res) {
                            //}
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layer.msg("下架成功");
                              table.reload('LAY-app-content-list2');
                          }
                        });
                    });
                },
                //加入团购
                groupbuy: function (othis) {
                    var checkStatus = table.checkStatus('LAY-app-content-list2')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    checkids = [];
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        if (eobj.IsShow_Sel == 1) {
                            tip = "未上架的商品不可加入团购";
                        } else if (eobj.GroupBuyInfo != null) {
                            tip = "选中商品正在团购时效内，无法再次加入团购";
                        } else {
                            checkids.push(eobj.PID);
                        }
                    });
                    if (tip != "") {
                        checkids = [];
                        return layer.msg(tip);
                    }

                    admin.popup({
                        title: '加入团购'
                      , skin: 'layui-layer-lan'
                      , area: ['60%', '75%']
                      , id: 'LAY-popup-content-addgroup'
                      , success: function (layero, index) {
                          view(this.id).render('/shop/shop_groupbuy_goods/groupbuy').done(function () {
                              form.render(null, 'layuiadmin-app-form-list');

                              //监听提交
                              form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                  var field = data.field; //获取提交的字段
                                  //提交 Ajax 成功后，关闭当前弹层并重载表格
                                  //请求登入接口
                                  admin.req({
                                      url: layui.setter.APIUrl + '/shop/Groupbuy_goodsMgr/AddGoods' //实际使用请改成服务端真实接口
                                    , data: field
                                    , type: "post"
                                    , done: function (res) {
                                        layer.close(index); //执行关闭
                                        layui.table.reload('LAY-app-content-list2'); //重载表格
                                        layer.msg('加入团购成功');
                                    }
                                  });
                              });
                          });
                      }
                    });
                },
                //取消团购
                cancelgroupbuy: function (othis) {
                    var checkStatus = table.checkStatus('LAY-app-content-list2')
                    , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    checkids = [];
                    var tip = "";
                    $.each(checkData, function (index, eobj) {
                        if (eobj.GroupBuyInfo == null) {
                            tip = "选中商品未在团购时效内，无法取消";
                        } else {
                            checkids.push(eobj.PID);
                        }
                    });
                    if (tip != "") {
                        checkids = [];
                        return layer.msg(tip);
                    }
                    var strgood = JSON.stringify(checkids);
                    layer.confirm('确定取消团购吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/Groupbuy_goodsMgr/CancelGoods' //实际使用请改成服务端真实接口
                          , data: { "GoodIds": strgood }
                          , type: "post"
                          , done: function (res) {
                              layer.close(index); //执行关闭
                              layui.table.reload('LAY-app-content-list2'); //重载表格
                              layer.msg('取消团购成功');
                          }
                        });
                    });
                }
            };

            $('.layui-btn.layuiadmin-btn-list2').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    }

    function GroupBuyInfo(price, stime, etime, isable, code) {
        if (isable == 0)
            isable = "显示"
        else
            isable = "不显示";;
        var content = "团购价：￥" + price;
        content += "<br />开始时间：" + stime;
        content += "<br />结束时间：" + etime;
        content += "<br />是否显示：" + isable;
        layer.tips(content, '#tip' + code, {
            tips: [1, '#5FB878'],
            time: 4000
        });
    }
</script>
